<?php
// 曾科修改，之前是王大任所写
require_once '../../configs/common.games.php';//加载数据库类文件

if (!array_key_exists($_SERVER['REMOTE_ADDR'], $trust_server)) {
	echo "fail:auth ".$_SERVER['REMOTE_ADDR'];
	exit;
}

$url = new Url();
$action = $url->get('action');
$user = $url->get('user');
$db = new Data();
$memcache = new Cache();

if ($action == "off") { //禁用账号
	$query=$db->query("select u_id,acc_key,off from player_info where `username`='".$user."'");
	$m=$db->fetch_array($query);

	if($m['off'] == 1) {//帐号已被禁
		echo "success";
		exit;
	}
	
	$key = $m['acc_key'];
	$UserId = $m["u_id"];

	$db->query("update player_info set off=1 where `u_id`='".$UserId."'");
	$memcache->delete("ku_".$key);
	$memcache->delete("kc_".$key);
	
	echo "success";
  exit;
} else if ($action == "on") { //解封
	$db->query("update player_info set off=0 where `username`='".$user."'");
	echo "success";
 	exit;
} else if($action == "setpwd") {
	$pwd=substr(md5($_GET['pwd']),0,16);//设置密码
	$db->query("update player_info set password='".$pwd."' where `username`='".$user."'");
	echo "success";
    exit;
} else if($action == "pay") {
	//get payment subreason for log
	//0 - other, 1 - ipn, 2 - tapjoy, 3 - paypal
	if (isset($_GET['ptype'])) {
		$paytype = intval($url->get('ptype'));
	} else {
		$paytype = 0;
	}

	$payvalue = $url->get('value');
	if ($payvalue) {
		$query=$db->query("select u_id,cashmoney,acc_key from player_info where `username`='".$user."'");
		$m=$db->fetch_array($query);
		$UserId = $m["u_id"];
		$origincash = $m["cashmoney"];
		$key=$m["acc_key"];
		$paycash = 0;
		if ($paytype != 0) {
			if(array_key_exists($payvalue, $payment_value)) { //TODO $payment_value 目前变量是干嘛的？
				$paycash = $payment_value[$payvalue]; 
			} else if($paytype == 2) {
				$paycash = floor($payvalue/15);//tapjoy
			} else {
				$paycash = floor($payvalue/10);//paypal?
			}
		}
		/*
1					0							0							充值
1					1							0							IPN充值
1					2							0							tapjoy充值
1					3							0							paypal充值
		*/
		log_gem($UserId, $origincash, $payvalue, 1, $paytype, 0);
		$db->query("update player_info set cashmoney=cashmoney+".$payvalue.",totalpay=totalpay+".$paycash." where `u_id`=".$UserId);
		$player_info = $memcache->get("ku_".$key);
		if ($player_info) {
			$player_info['cashmoney']+=$payvalue;
			$player_info['totalpay']+=$paycash;
			$memcache->set("ku_".$key, $player_info);
			$eventmsg = array("typeid"=>9);
			$memcache->setEvent($memcache, $UserId, $eventmsg);
		}
	}
	echo "success";
    exit;
}
else if ($action == "email") {
	$email = $_GET['email'];
	if ($email) {
		$query=$db->query("select u_id,acc_key from player_info where `username`='".$user."'");
		$m=$db->fetch_array($query);
		$UserId = $m["u_id"];
		$key=$m["acc_key"];
		
		$db->query("update player_info set email='".$email."' where `u_id`='".$UserId."'");
		$player_info=$memcache->get("ku_".$key);
		if ($player_info) {
			$player_info['email']=$email;
			$memcache->set("ku_".$key, $player_info);
		}
	}
	
	echo "success";
  	exit;
} else if($action == "gift") {
	$str_items = $_GET['items'];
	if ($str_items) {
		if($user) {
			$items = explode(",",$str_items);
			$query=$db->query("select u_id from player_info where `username`='".$user."'");
			$m=$db->fetch_array($query);
			$UserId = $m["u_id"];

			foreach ($items as $itemid) {
				giveItemReward($memcache, $db, 1, $itemid, $UserId); //TODO 该函数在哪里？
			}
			echo "success";
		}
	}
	exit;
} else if($action == "fbgift") { //not supported
	echo "fail:unknown";
	exit;
}
?>
